# -*- encoding: utf-8 -*-
'''
file       :2_generate_pic.py
Description: 形成5*5个色块中只有中心点是白色, 而包围是黑色的图像
Date       :2022/12/21 14:52:58
Author     :Josco
version    :python3.7.8
'''
from config import n, width, height, standard_pic, wait_pic_time, windowname # 从config中读取标准图像n,width,height以及标准图像在初始化时在屏幕上成像的时间
from public import *

class generate_standard_pic_method(object):
    """
        desc: 在每次初始化的时候需要将标准图像先进行展示, 并由解码部分控制摄像机进行拍摄,
                用于解码部分的动态点位像素取值作用
    """
    def __init__(self) -> None:
        pass

    def generate_st_pic(self):
        """
            desc: 形成标准图像, 并按照start_wait_key时间进行初始化展示
        """
        # # 这是五色的标准图像矩阵生成bit流（单个5*5块的组成部分）
        # point_color_list_1 = np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
        # point_color_list_2 = np.array([0,0,0,0,0,0,1,1,1,0,0,0,0,0,0])

        # 这是七色的标准图像矩阵生成bit流（单个7*7块的组成部分）
        point_color_list_1 = np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
        point_color_list_2 = np.array([0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0])
                            
        all_point_matrix = np.array([])
        len_list = int(len(list(point_color_list_1)) / 3)
        yushu = int(len_list / 2)
        
        for _row in range(int(height/3)):
            row_point_matrix = np.array([])
            for i in range(len_list):
                col_point_matrix = np.array([])
                for _col in range(width):
                    if i != yushu:
                        col_point_matrix = np.hstack((col_point_matrix,point_color_list_1))
                    else:
                        col_point_matrix = np.hstack((col_point_matrix,point_color_list_2))
                row_point_matrix = np.hstack((row_point_matrix, col_point_matrix))
            all_point_matrix = np.hstack((all_point_matrix, row_point_matrix))
        all_point_matrix = all_point_matrix.reshape((-1,width*n,3))
        all_point_matrix = all_point_matrix * 255
        all_point_matrix = all_point_matrix.astype("uint8")

        # 添加黑色包围
        black_margin_top = np.zeros((n,all_point_matrix.shape[1],3),dtype="uint8")
        all_point_matrix = np.concatenate((black_margin_top,all_point_matrix,black_margin_top),axis=0)
        black_margin_left = np.zeros((all_point_matrix.shape[0],n,3),dtype="uint8")
        all_point_matrix = np.concatenate((black_margin_left,all_point_matrix,black_margin_left),axis=1)
        # 添加白色包围
        white_margin_top = np.zeros((n,all_point_matrix.shape[1],3),dtype="uint8")
        white_margin_top = white_margin_top*255
        all_point_matrix = np.concatenate((white_margin_top,all_point_matrix,white_margin_top),axis=0)
        white_margin_left = np.zeros((all_point_matrix.shape[0],n,3),dtype="uint8")
        white_margin_left = white_margin_left*255
        all_point_matrix = np.concatenate((white_margin_left,all_point_matrix,white_margin_left),axis=1)
        
        # 将3大块拼接成一个相机对应的完整图像
        n3_point_matrix = np.concatenate((all_point_matrix,all_point_matrix,all_point_matrix),axis=0)
        
        # 将3个相机对应的完整图像拼接成在屏幕展示时所看到的完整的9个大块的图像并存储
        n9_point_matrix = np.concatenate((n3_point_matrix,n3_point_matrix,n3_point_matrix),axis=1)
        cv.imwrite(standard_pic, n9_point_matrix)
        print("现在开始生成并展示标准图像111111111111111")

        # 获取cv屏幕绘图资源，进行绘图初始化操作
        h, w, c = n9_point_matrix.shape
        resize_width = w
        resize_height = h
        cv.namedWindow(windowname, cv.WINDOW_NORMAL)
        cv.resizeWindow(windowname, resize_width, resize_height)

        show_pic(n9_point_matrix, wait_pic_time)
        print("标准图像生成并展示完毕111111111111111")
